import { SDKState, ResultCallback, SDKDir, ADDir, LoadCallback, SDKLogicState, ResultState, NativeAdCallback, SDKAdParam, SDKStyle } from "../../SDKDefine";
import BindFunction from "../../tools/BindFunction";
import SDKEvent from "../../tools/SDKEvent";
import { BaseSDK } from "./BaseSDK";
import { IAdAdapter } from "./IAdAdapter";
/**
 * 所有广告的父类
 * 子类不可以覆盖父类的方法。
 * 
 */
export default abstract class BaseAd extends BaseSDK implements IAdAdapter {

    protected name: string = ''
    //广告状态
    private state: SDKState = SDKState.close;
    //当前使用的广告ID
    protected adUnitID: string = ''
    //游戏逻辑状态
    private logicState: SDKLogicState = SDKLogicState.close

    protected ad: any;

    protected funcHelper: BindFunction = new BindFunction()

    // //创建时的时间戳
    // protected initTime: number = 0;
    // //开始展示的时间戳
    // protected startTime: number = 0;
    // //展示时长 毫秒
    // protected showLong: number = 0

    protected param: SDKAdParam = {
        index: 0,
        dir: SDKDir.BOTTOM_MID,
        count: 1,
        orientation: ADDir.vertical
    }

    protected style: SDKStyle = {

        width: 300, height: 100,

    };


    setStyle(s: SDKStyle) {
        if (s) {
            for (const key in s) {
                if (s.hasOwnProperty(key)) {
                    const element = s[key];
                    this.style[key] = element;
                }
            }
        }
    }

    getStyle(): SDKStyle {
        return this.style;
    }

    setID(id: string) {
        this.adUnitID = id;
    }

    isLogicState(s: SDKLogicState) {
        return this.logicState == s;
    }





    isState(s: number) {
        return this.state == s
    }




    getFunc(func: Function) {
        return this.funcHelper.getFunc(func, this)
    }




    //预加载广告
    preload(): void {
        this.setLogicState(SDKLogicState.close)
        this.create();
    }
    //关闭广告
    close(): void {

    }

    create(): void {

    }
    //销毁
    destroy(): void {

    }
    //初始化广告
    init(): void {

    }
    //显示广告
    open(): void {

    }

    onLoad() {
        this.setState(SDKState.loadSucess)
        this.onLoadLogic();
    }

    protected onLoadLogic() {

    }

    onError(err: any) {
        this.setState(SDKState.loadFail)
        this.onErrorLogic();
    }

    protected onErrorLogic() {

    }



    protected callResultCallback(s: ResultState) {
        let callback = this.param.resultCallback;
        if (callback) {
            callback(s)
            this.param.resultCallback = null;
        }
    }

    setParam(param: SDKAdParam): void {
        for (const key in param) {
            if (param.hasOwnProperty(key)) {
                const element = param[key];
                this.param[key] = element;
            }
        }
        this.setStyle(param.sytle)
        this.updateSize()
    }

    setState(s: SDKState): void {
        this.state = s;
        console.log(' state is ', s, this.name)
        if (this.param.loadCallback) {
            this.param.loadCallback(s)
        }
    }

    protected setLogicState(s: SDKLogicState) {
        this.logicState = s;
    }

    protected pause() {
        SDKEvent.instance().emit(SDKEvent.REWARD_AD_OPEN)
    }

    protected resume() {
        SDKEvent.instance().emit(SDKEvent.REWARD_AD_CLOSE)
    }

    updateSize() {

    }
}